﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserDefaultCompany.aspx.cs" Inherits="CRMWeb.Pages.Admin.UserDefaultCompany" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>User Default Company</title>
</head>
<body>
    <table id="dgDefCompany" title="User Default Company" style="width: 600%; height: 380px; overflow-y: auto;"
        toolbar="#toolbarDefault" pagination="true" data-options="pageSize:10,pageList: [10,20,30,50]" rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true">
        <thead>
            <tr>
                <th field="UserName" width="200" align="center">UserName</th>
                <th field="DefaultCompanyName" width="500px" align="center">DefaultCompanyName</th>
            </tr>
        </thead>
    </table>
    <div id="toolbarDefault">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newDefCom()">New</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editDefCom()">Edit</a>
        <a id="lnkDelete" href="javascript:void(0)" class="easyui-linkbutton" onclick="openDelDCPopup()" plain="true" iconcls="icon-remove" title="Delete Default Company Setting.">Delete</a>
    </div>
    <div id="dlgDefComSetting" class="easyui-dialog" style="width: 420px; height: 180px; padding: 10px"
        closed="true" buttons="#dlg-buttons">
        <form id="fmdlgSetting" method="post" runat="server" novalidate>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">User:</label>
                <input class="easyui-combobox" id="cmbUsers" style="width: 267px;" required="required"
                    data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'NodeId',  
                        textField: 'MemberName',
                        url: '/WebServices/DefaultCompany.ashx?method=BindDefaultUsers',
                    selected:true" />
            </div>
            <div class="fitem" style="margin-bottom: 10px">
                <label style="width: 97px;">Company:</label>
                <input class="easyui-combobox" id="cmbCompanies" style="width: 267px;" required="required" data-options="panelWidth:265,  
                    editable:false,
                        valueField: 'NodeID',  
                        textField: 'MemberGroupName',
                        url: '/WebServices/DefaultCompany.ashx?method=BindAllDefCompany',
                    selected:true" />
            </div>
            <input id="hdnID" value="" style="display: none" />
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgDefComSetting').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveDefCom()">Save</a>
    </div>
    
    <script type="text/javascript">

        //Open new settings dialog:
        function newDefCom() {
            //Open panel on text area click:
            $("#cmbUsers").combobox('reload');
            $("#cmbCompanies").combobox('reload');
            $('#cmbUsers').combobox('textbox').bind('click', function () {
                $('#cmbUsers').combobox('showPanel');
            });

            $('#cmbCompanies').combobox('textbox').bind('click', function () {
                $('#cmbCompanies').combobox('showPanel');
            });

            $('#hdnID').val('');
            $('#dlgDefComSetting').dialog('open').dialog('setTitle', 'New Default User Company Setting');
            $('#fmdlgSetting').form('clear');
            url = '/WebServices/DefaultCompanyHandler/AddDefaultCompany';
        }
        //Resize datagrid:
        window.onresize = function () {
            $('#dgDefCompany').datagrid('resize', {
                width: window.innerWidth - 165,
                height: window.innerHeight - 40
            });
        }
        //Edit default settings:
        function editDefCom() {
            var row = $('#dgDefCompany').datagrid('getSelected');
            $('#cmbUsers').combobox('textbox').bind('click', function () {
                $('#cmbUsers').combobox('showPanel');
            });

            $('#cmbCompanies').combobox('textbox').bind('click', function () {
                $('#cmbCompanies').combobox('showPanel');
            });
            if (row) {

                $('#dlgDefComSetting').dialog('open').dialog('setTitle', 'Edit Default User Company Setting');
                $('#fmdlgSetting').form('load', row);
                $('#hdnID').val(row.UsercompanyID);
                $('#cmbUsers').combobox('setValue', row.UserID);
                $('#cmbCompanies').combobox('setValue', row.UserDefaultgroupID);
                url = '/WebServices/DefaultCompanyHandler/EditDefaultCompany?UsercompanyID=' + row.UsercompanyID;
            }
        }

        //Open delete dialog if row is selected:
        function openDelDCPopup() {
            var row = $('#dgDefCompany').datagrid('getSelected');
            if (row) {
                $('#headertext').html("Are you sure want to delete this Default Company Setting?");
                $('#dlgDelconfirm').dialog('open');
                $('#btnDelete').bind("click", function () {
                    deleteDefCom();
                });
            }
        }

        //Validate required fields
        function IsValidate() {
            var flag = true;
            if ($('#cmbUsers').combobox('getValue') == '') {
                $.messager.alert('Morgan CRM', 'Please select user...!', 'warning');
                return flag = false;
            }
            if ($('#cmbCompanies').combobox('getValue') == '') {
                $.messager.alert('Morgan CRM', 'Please select company...!', 'warning');
                return flag = false;
            }
          
            return flag;
        }

        //Delete selected row:
        function deleteDefCom() {
            var row = $('#dgDefCompany').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/DefaultCompany.ashx',
                    type: 'POST',
                    data: {
                        method: 'DeleteDefaultCompany',
                        args: { UsercompanyID: row.UsercompanyID }
                    },
                    success: function (data) {
                        $('#dgDefCompany').datagrid('reload');
                        $('#dlgDelconfirm').dialog('close');
                    }
                });
            }
        }

        //Load default settings into datagrid:
        $(document).ready(function () {
            $('#dgDefCompany').datagrid({
                url: '/WebServices/DefaultCompany.ashx?method=GetDefaultSetting',
                onDblClickRow: function (index, rowdata) {
                    editDefCom();
                }
            });
        });


        //Save or Edit default company settings:
        function saveDefCom() {
            if (IsValidate()) {
            var UsercompanyID = $('#hdnID').val();
            var UserID = $('#cmbUsers').combobox('getValue');
            var UserDefaultgroupID = $('#cmbCompanies').combobox('getValue');

            if (UsercompanyID != '') {
                if (UserID != '') {
                    $.ajax({
                        url: '/WebServices/DefaultCompany.ashx',
                        type: 'POST',
                        data: {
                            method: 'EditDefaultCompany',
                            args: { UsercompanyID: UsercompanyID, UserID: UserID, UserDefaultgroupID: UserDefaultgroupID }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgDefComSetting').dialog('close');
                                $('#dgDefCompany').datagrid('reload');
                            }
                            else {
                                $.messager.alert('Morgan CRM', 'Default company setting already exist.', 'info');

                            }
                        }
                    });
                }
            }
            else {
                $.ajax({
                    url: '/WebServices/DefaultCompany.ashx',
                    type: 'POST',
                    data: {
                        method: 'AddDefaultCompany',
                        args:
                            { UserID: UserID, UserDefaultgroupID: UserDefaultgroupID }
                    },
                    success: function (data) {
                        if (data == 1) {
                            $('#dlgDefComSetting').dialog('close');
                            $('#dgDefCompany').datagrid('reload');
                        }
                        else {
                            $.messager.alert('Morgan CRM', 'Default company setting already exist.', 'info');
                        }
                    }
                });

            }}
        }

    </script>
</body>
</html>

